//
//  GoodsViewController.m
//  星期6
//
//  Created by 宗镇荣 on 16/2/22.
//  Copyright © 2016年 zzr. All rights reserved.
//

#import "GoodsViewController.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"
#import "GoodsModel+net.h"
#import "CommonDefin.h"
#import "GoodsViewCell.h"
#import "UIImageView+WebCache.h"
#import "DetailGoodsViewController.h"

@interface GoodsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic) NSMutableArray *dataSource;
@property (nonatomic, assign)NSInteger currentPage;
@property (nonatomic) UICollectionView *collectionView;
@property (nonatomic) MBProgressHUD *hud;

@end

@implementation GoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self refreshData];
    [self configUI];
}

- (void)refreshData {

    _currentPage = 1;
    [self loadData];
}

- (void)loadMoreData {
    
    _currentPage++;
    [self loadData];
}

- (void)loadData {

    [self.view addSubview:self.hud];
    [self.hud show:YES];
    [GoodsModel requestPage:_currentPage completionBlock:^(NSMutableArray *array, NSError *error) {
        
        if (error == nil) {
            
            if (_currentPage == 1) {
                
                [self.dataSource removeAllObjects];
            }
            [self.dataSource addObjectsFromArray:array];
            [self.collectionView reloadData];
            
            [_collectionView.header endRefreshing];
            [_collectionView.footer endRefreshing];
        }else {
            
            NSLog(@"%@", error);
        }
        
        [self.hud hide:YES];
    }];

}

- (void)configUI {

    [self setNavigationTitle:@"单品"];
    UICollectionViewFlowLayout *flowlayOut = [[UICollectionViewFlowLayout alloc]init];
    flowlayOut.sectionInset = UIEdgeInsetsMake(0, 10, 5, 10);
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.view.frame.size.height - 64 - 49 ) collectionViewLayout:flowlayOut];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    
    [_collectionView registerNib:[UINib nibWithNibName:@"GoodsViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"MYCELL"];
    
    _collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self refreshData];
    }];
    _collectionView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        [self loadMoreData];
    }];

}

#pragma  mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    GoodsViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MYCELL" forIndexPath:indexPath];
    GoodsModel *model = self.dataSource[indexPath.row];
    [cell.goodsImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_image_url]];
    cell.goodNameLabel.text = model.name;
    //NSLog(@"帅哥   爱 美女  %@", model.name);
     NSAttributedString *string = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥ %@",model.price] attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleNone)}];
    cell.goodMoneyLabel.attributedText = string;
    
    // cell.goodMoneyLabel.text = model.price;
    //cell.goodsLikeLabel.text = [NSString stringWithFormat:@"%@", model.favorites_count];
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    GoodsModel *model = self.dataSource[indexPath.row];
    DetailGoodsViewController *detail = [[DetailGoodsViewController alloc] init];
    detail.url = model.url;
    detail.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:detail animated:NO];
}

#pragma  mark UICollectionViewDelegateFlowLayout
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kScreenWidth-50)/2, (kScreenWidth-50)/2);
    
}

#pragma mark - 懒加载
- (NSMutableArray *)dataSource {
    
    if (!_dataSource) {
        
        _dataSource = [[NSMutableArray alloc] init];
    }
    return _dataSource;
}
- (MBProgressHUD *)hud {
    
    if (!_hud) {
        
        _hud = [[MBProgressHUD alloc] initWithView:self.view];
        _hud.labelText = @"正在加载";
    }
    return _hud;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
